package com.admin.controller;


import cn.hutool.core.io.FileUtil;
import com.admin.entity.WelcomePage;
import com.admin.service.IWelcomePageService;
import com.admin.service.impl.WelcomePageServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bean.ResponseBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.File;


/**
 * <p>
 *  前端控制器
 * </p>
 *  欢迎界面
 * @author marisheng
 * @since 2019-11-21
 */
@RestController
@RequestMapping("/admin/welcome-page")
public class WelcomePageController {

    @Value("${upload.dir}")
    private String tmp;

    @Autowired
    HttpServletRequest request;

    @Autowired
    private IWelcomePageService welcomePageService;

    /**
     * 添加欢迎页
     * @param welcomePage
     * @return
     */
    @RequestMapping(value = "add/welcomepage",method = RequestMethod.POST)
    public ResponseBean addWelcomePage(@RequestBody WelcomePage welcomePage){

        int i = welcomePageService.addHuanYingYe(welcomePage);

        ResponseBean responseBean=null;

        if(i>0){
            String path = request.getServletContext().getRealPath("images");

            File file=new File(path);

            if(file.exists()==false){
                file.mkdirs();
            }
            FileUtil.copy(tmp+"/"+welcomePage.getWelcomePageFile(),path+"/"+welcomePage.getWelcomePageFile(),true);
            responseBean=new ResponseBean(200,"操作成功","");
        }else{
            responseBean=new ResponseBean(500,"操作失败","");
        }
        return responseBean;
    }


    /**
     * 修改欢迎页面的配置
     * @param welcomePage
     * @param result 处理message
     * @return
     */
    @PutMapping("/updateWelcomePage")
    public ResponseBean updateWelcomePage(@Valid @RequestBody WelcomePage welcomePage, BindingResult result) {
        if (result.hasErrors()){
            return new ResponseBean(500,"操作失败",result.getFieldError().getDefaultMessage()) ;
        }
        //判断欢迎页面查站点Id 是否存在
        boolean byId = welcomePageService.findStationId(welcomePage.getWelcomePageStation());
        if (byId == false) {
            return new ResponseBean(500, "操作失败", "请输入正确的修改条件");
        }

        Boolean f = welcomePageService.updateWelcomePage(welcomePage);
        int code = f ? 200 : 500;
        if (code == 200) {
            return new ResponseBean(200, "修改成功", null);
        } else {
            return new ResponseBean(500, "修改失败", null);
        }
    }

    /**
     * 查询生效的欢迎界面的站点Id
     * @param stationId
     * @return
     */
    @GetMapping("/welcomePage")
    public ResponseBean welcomePage(Integer stationId){
        if (stationId==null){
            return new ResponseBean(500,"查询失败","未获取到编号");
        }
        // 判断站点ID存在
        WelcomePage welcomePage = welcomePageService.queryWelcomePage(stationId);
        if (welcomePage==null){
            return new ResponseBean(500,"查询失败","无用户无信息");
        }

        return new ResponseBean(200,"success",welcomePage);
    }

}
